﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WoSoft.PACS.Data.Models;
using System.IO;
using WoSoft.PACS.Data.TemplateData;
using WoSoft.PACS.Data.CheckItem;

namespace WoSoft.PACS.Win.Forms.SubForms
{
    public partial class FrmArcheTypeInfo : FrmBase
    {
        private string _templateID = string.Empty;
        public FrmArcheTypeInfo(string templateID)
        {
            InitializeComponent();
            _templateID = templateID;
        }

        //加载模板
        private void FrmArcheTypeInfo_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            //设置为拼音码
            this.SetSpellCodeControl(this.tbName, this.tbCode);

            //加载检查类别
            CheckItemDataService checkItemDataService = new CheckItemDataService();
            var dt = checkItemDataService.GetCheckItemFlAllData();
            cbCheckType.DataSource = dt;
            DataRow row = dt.NewRow();
            row["ID"] = "F00B6FBA-E14E-469F-B06D-A9FFBA15BF21";
            row["名称"] = "所有";
            dt.Rows.Add(row);
            cbCheckType.DisplayMember = "名称";
            cbCheckType.ValueMember = "ID";
            cbCheckType.SelectedIndex = -1;
        }

        //打开文件对象框
        private void tbFile_ButtonCustomClick(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "Word模块文件|*.doc|所有word格式文件|*.docx";
            openFile.Multiselect = false;
            var r = openFile.ShowDialog(this);
            if (r == System.Windows.Forms.DialogResult.OK)
            {
                this.tbFile.Text = openFile.FileName;
                this.tbName.Text = Path.GetFileName(this.tbFile.Text);
            }

        }

        //确证检查
        private void btnOK_Click(object sender, EventArgs e)
        {
            this.errorProvider1.Clear();

            if (this.cbCheckType.Text.Trim() == string.Empty)
            {
                this.errorProvider1.SetError(this.cbCheckType, "请录入或选择报告类型");
                return;
            }

            if (this.tbName.Text.Trim() == string.Empty)
            {
                this.errorProvider1.SetError(this.tbName, "请填写名称");
                return;
            }

            if (this.tbFile.Text.Trim() == string.Empty)
            {
                this.errorProvider1.SetError(this.tbFile, "请选择文件模板");
                return;
            }

            var dat = fileData(this.tbFile.Text.Trim());
            if (dat != null)
            {
                //原型
                var model = new TemplateModel()
                {
                    ID = string.IsNullOrEmpty(_templateID) ? Guid.NewGuid().ToString() : _templateID,
                    Code = this.tbCode.Text.Trim(),
                    Name = this.tbName.Text.Trim(),
                    Type = this.cbCheckType.Text.Trim(),
                    Content = dat,
                    IsDefault = this.ckIsDefault.Checked
                };


                var templateService = new TemplateDataService();
                var cnt = 0;
                if (string.IsNullOrEmpty(_templateID))
                {
                    cnt = templateService.Insert(model);
                }
                else
                {
                    cnt = templateService.Update(model);
                }


                if (cnt > 0)
                {
                    DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
                else
                {
                    this.errorProvider1.SetError(this.btnOK, "保存错误,请检查数据");
                }
            }

        }

        //获取文件字节
        private byte[] fileData(string fileName)
        {
            if (File.Exists(fileName))
            {
                var fs = File.OpenRead(fileName);
                var len = (int)fs.Length;
                var data = new byte[len];
                var cnt = fs.Read(data, 0, len);
                fs.Close();
                return data;
            }
            return null;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
